package com.mdd.admin.service.impl.yylq;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.yylq.ITAppDicService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.validate.yylq.TAppDicCreateValidate;
import com.mdd.admin.validate.yylq.TAppDicSearchValidate;
import com.mdd.admin.validate.yylq.TAppDicUpdateValidate;
import com.mdd.admin.vo.yylq.TAppDicDetailVo;
import com.mdd.admin.vo.yylq.TAppDicListedVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.yangyangLeadingEnterprise.TAppDic;
import com.mdd.common.mapper.yangyangLeadingEnterprise.TAppDicMapper;
import com.mdd.common.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 字典实现类
 * @author LikeAdmin
 */
@Service
public class TAppDicServiceImpl implements ITAppDicService {
        
    @Resource
    TAppDicMapper tAppDicMapper;

    /**
     * 字典列表
     *
     * @author LikeAdmin
     * @param pageValidate 分页参数
     * @param searchValidate 搜索参数
     * @return PageResult<TAppDicListedVo>
     */
    @Override
    public PageResult<TAppDicListedVo> list(PageValidate pageValidate, TAppDicSearchValidate searchValidate) {
        Integer page  = pageValidate.getPage_no();
        Integer limit = pageValidate.getPage_size();

        QueryWrapper<TAppDic> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc(Arrays.asList("type","sort", "id"));

        tAppDicMapper.setSearch(queryWrapper, searchValidate, new String[]{
            "like:name:str",
            "=:type:str",
            "=:typeValue@type_value:str",
            "=:remark:str",
            "=:sort:int",
            "=:status:int",
            "=:parentId@parent_id:int",
        });

        IPage<TAppDic> iPage = tAppDicMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<TAppDicListedVo> list = new LinkedList<>();
        for(TAppDic item : iPage.getRecords()) {
            TAppDicListedVo vo = new TAppDicListedVo();
            BeanUtils.copyProperties(item, vo);
            vo.setCreateTime(TimeUtils.formatDate(item.getCreateTime()));
            vo.setUpdateTime(TimeUtils.formatDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 字典详情
     *
     * @author LikeAdmin
     * @param id 主键参数
     * @return TAppDic
     */
    @Override
    public TAppDicDetailVo detail(Integer id) {
        TAppDic model = tAppDicMapper.selectOne(
                new QueryWrapper<TAppDic>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        TAppDicDetailVo vo = new TAppDicDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 字典新增
     *
     * @author LikeAdmin
     * @param createValidate 参数
     */
    @Override
    public void add(TAppDicCreateValidate createValidate) {
        TAppDic model = new TAppDic();
        model.setName(createValidate.getName());
        model.setType(createValidate.getType());
        model.setTypeValue(createValidate.getTypeValue());
        model.setRemark(createValidate.getRemark());
        model.setSort(createValidate.getSort());
        model.setStatus(createValidate.getStatus());
        model.setCreateTime(new Date());
        model.setUpdateTime(new Date());
        model.setParentId(createValidate.getParentId());
        tAppDicMapper.insert(model);
    }

    /**
     * 字典编辑
     *
     * @author LikeAdmin
     * @param updateValidate 参数
     */
    @Override
    public void edit(TAppDicUpdateValidate updateValidate) {
        TAppDic model = tAppDicMapper.selectOne(
                new QueryWrapper<TAppDic>()
                    .eq("id",  updateValidate.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(updateValidate.getId());
        model.setName(updateValidate.getName());
        model.setType(updateValidate.getType());
        model.setTypeValue(updateValidate.getTypeValue());
        model.setRemark(updateValidate.getRemark());
        model.setSort(updateValidate.getSort());
        model.setStatus(updateValidate.getStatus());
        model.setUpdateTime(new Date());
        model.setParentId(updateValidate.getParentId());
        tAppDicMapper.updateById(model);
    }

    /**
     * 字典删除
     *
     * @author LikeAdmin
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        TAppDic model = tAppDicMapper.selectOne(
                new QueryWrapper<TAppDic>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        tAppDicMapper.delete(new QueryWrapper<TAppDic>().eq("id", id));
    }

    @Override
    public List<TAppDic> selectDicListByType(String type) {
        QueryWrapper<TAppDic> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type).eq("status", 1).orderByDesc("sort");
        List<TAppDic> dicList = tAppDicMapper.selectList(wrapper);
        return Optional.ofNullable(dicList).orElse(new ArrayList<>());
    }

}
